using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class frmTimKiemHocSinh : Form
    {
        private HocSinhCrt hsCrt;

        public frmTimKiemHocSinh()
        {
            InitializeComponent();
        }

        #region Load Data

        private void LoadLopHoc()
        {
            LopHocBus lhBus = new LopHocBus();
            IList ds;
            ds = lhBus.LayDanhSach();
            cmbLopHoc.DataSource = ds;
            cmbLopHoc.ValueMember = "Ma";
            cmbLopHoc.DisplayMember = "Ten";
        }

        private void CapNhatTieuChuanTraCuu(bool bUpdated)
        {
            if (bUpdated)
            {
                if ((txtMaHocSinh.Text != ""))
                {
                    hsCrt.Ma = int.Parse(txtMaHocSinh.Text);
                }
                else
                {
                    hsCrt.Ma = 0;
                }
                hsCrt.Ten = txtTenHocSinh.Text;
                hsCrt.NgaySinhTu = dtpNgaySinhTu.Value.Date;
                hsCrt.NgaySinhDen = dtpNgaySinhDen.Value.Date;
                hsCrt.DiaChi = txtDiaChi.Text;
                hsCrt.ToanTu = (double) nudDiemToanTu.Value;
                hsCrt.ToanDen = (double) nudDiemToanDen.Value;
                hsCrt.LyTu = (double) nudDiemLyTu.Value;
                hsCrt.LyDen = (double) nudDiemLyDen.Value;
                hsCrt.HoaTu = (double) nudDiemHoaTu.Value;
                hsCrt.HoaDen = (double) nudDiemHoaDen.Value;
                hsCrt.DTBTu = (double) nudDTBTu.Value;
                hsCrt.DTBDen = (double) nudDTBDen.Value;
                hsCrt.CheckNgaySinh = chkNgaySinh.Checked;
                hsCrt.CheckDiaChi = chkDiaChi.Checked;
                hsCrt.CheckToan = chkDiemToan.Checked;
                hsCrt.CheckLy = chkDiemLy.Checked;
                hsCrt.CheckHoa = chkDiemHoa.Checked;
                hsCrt.CheckDTB = chkDTB.Checked;
                hsCrt.CheckLopHoc = chkLopHoc.Checked;
            }
            else
            {
                if ((hsCrt.Ma != 0))
                {
                    txtMaHocSinh.Text = hsCrt.Ma.ToString();
                }
                txtTenHocSinh.Text = hsCrt.Ten;
                dtpNgaySinhTu.Value = hsCrt.NgaySinhTu.Date;
                dtpNgaySinhDen.Value = hsCrt.NgaySinhDen.Date;
                txtDiaChi.Text = hsCrt.DiaChi;
                nudDiemToanTu.Value = (decimal) hsCrt.ToanTu;
                nudDiemToanDen.Value = (decimal) hsCrt.ToanDen;
                nudDiemLyTu.Value = (decimal) hsCrt.LyTu;
                nudDiemLyDen.Value = (decimal) hsCrt.LyDen;
                nudDiemHoaTu.Value = (decimal) hsCrt.HoaTu;
                nudDiemHoaDen.Value = (decimal) hsCrt.HoaDen;
                nudDTBTu.Value = (decimal) hsCrt.DTBTu;
                nudDTBDen.Value = (decimal) hsCrt.DTBDen;
                chkNgaySinh.Checked = hsCrt.CheckNgaySinh;
                chkDiaChi.Checked = hsCrt.CheckDiaChi;
                chkDiemToan.Checked = hsCrt.CheckToan;
                chkDiemLy.Checked = hsCrt.CheckLy;
                chkDiemHoa.Checked = hsCrt.CheckHoa;
                chkDTB.Checked = hsCrt.CheckDTB;
                chkLopHoc.Checked = hsCrt.CheckLopHoc;
            }
        }

        private void frmTimKiemHocSinh_Load(object sender, EventArgs e)
        {
            try
            {
                LoadLopHoc();
                hsCrt = new HocSinhCrt();
                CapNhatTieuChuanTraCuu(false);
                DataTable dtHS = new DataTable();
                dtgHocSinh.DataSource = dtHS;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion Load Data

        #region Check Criteria

        private void chkNgaySinh_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkNgaySinh.Checked)
            {
                dtpNgaySinhTu.Enabled = true;
                dtpNgaySinhDen.Enabled = true;
            }
            else
            {
                dtpNgaySinhTu.Enabled = false;
                dtpNgaySinhDen.Enabled = false;
            }
        }

        private void chkDiaChi_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkDiaChi.Checked)
            {
                txtDiaChi.Enabled = true;
            }
            else
            {
                txtDiaChi.Enabled = false;
            }
        }

        private void chkDiemToan_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkDiemToan.Checked)
            {
                nudDiemToanTu.Enabled = true;
                nudDiemToanDen.Enabled = true;
            }
            else
            {
                nudDiemToanTu.Enabled = false;
                nudDiemToanDen.Enabled = false;
            }
        }

        private void chkDiemLy_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkDiemLy.Checked)
            {
                nudDiemLyTu.Enabled = true;
                nudDiemLyDen.Enabled = true;
            }
            else
            {
                nudDiemLyTu.Enabled = false;
                nudDiemLyDen.Enabled = false;
            }
        }

        private void chkDiemHoa_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkDiemHoa.Checked)
            {
                nudDiemHoaTu.Enabled = true;
                nudDiemHoaDen.Enabled = true;
            }
            else
            {
                nudDiemHoaTu.Enabled = false;
                nudDiemHoaDen.Enabled = false;
            }
        }

        private void chkDTB_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkDTB.Checked)
            {
                nudDTBTu.Enabled = true;
                nudDTBDen.Enabled = true;
            }
            else
            {
                nudDTBTu.Enabled = false;
                nudDTBDen.Enabled = false;
            }
        }

        private void chkLopHoc_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkLopHoc.Checked)
            {
                cmbLopHoc.Enabled = true;
            }
            else
            {
                cmbLopHoc.Enabled = false;
            }
        }

        #endregion Check Criteria

        #region Xu Ly Su Kien

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
            try
            {
                CapNhatTieuChuanTraCuu(true);
                HocSinhBus hsBus = new HocSinhBus();
                DataTable dtHS;
                dtHS = hsBus.TimKiem(hsCrt);
                dtgHocSinh.DataSource = dtHS;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion Xu Ly Su Kien
    }
}